package com.luoke.dao.impl;


import cn.hutool.log.StaticLog;
import com.luoke.context.GlobalDataStore;
import com.luoke.dao.BatterPetDao;
import com.luoke.pojo.dto.BattleInfoDTO;
import com.luoke.pojo.entity.BattlePet;
import com.luoke.util.DatabaseUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : 叶非凡
 * @date : 2024/11/14 17:05
 */
public class BattlePetDaoImpl implements BatterPetDao {
    @Override
    public List<BattlePet> getBatterPetList(Long id, String userName) {
        List<BattlePet> list = new ArrayList<>();
        String sql = "SELECT * from  battle_pet where battle_id =? and is_active = 0  and is_alive=1 and owner_player_id=? ";
        try (Connection connection = DatabaseUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            // 设置 SQL 参数
            preparedStatement.setLong(1, GlobalDataStore.getBattleIdByPlayerName(userName));
            preparedStatement.setLong(2, id);
            // 执行 SQL
            try (ResultSet resultSet = preparedStatement.executeQuery()) {

                while (resultSet.next()) {
                    BattlePet battlePet = new BattlePet();
                    battlePet.setId(resultSet.getLong("id"));
                    battlePet.setBattleId(resultSet.getLong("battle_id"));
                    battlePet.setOwnerPlayerId(resultSet.getLong("owner_player_id"));
                    battlePet.setPetId(resultSet.getLong("pet_id"));
                    battlePet.setHp(resultSet.getInt("hp"));
                    battlePet.setMp(resultSet.getInt("mp"));
                    battlePet.setIsActive(resultSet.getInt("is_active"));
                    battlePet.setIsAlive(resultSet.getInt("is_alive"));
                    battlePet.setPosition(resultSet.getInt("position"));
                    battlePet.setPetName(resultSet.getString("pet_name"));

                    list.add(battlePet);
                }
            }

        } catch (SQLException e) {
            StaticLog.error(e.getMessage());
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public BattlePet getOtherBattlePet(Long id, String oldUserName) {

        BattlePet battlePet = new BattlePet();
        String sql = "SELECT * from  battle_pet where battle_id =?  and owner_player_id=? and is_active=1 ";
        try (Connection connection = DatabaseUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            // 设置 SQL 参数
            preparedStatement.setLong(1, GlobalDataStore.getBattleIdByPlayerName(oldUserName));
            preparedStatement.setLong(2, id);
            // 执行 SQL
            try (ResultSet resultSet = preparedStatement.executeQuery()) {

                if (resultSet.next()) {
                    battlePet.setId(resultSet.getLong("id"));
                    battlePet.setBattleId(resultSet.getLong("battle_id"));
                    battlePet.setOwnerPlayerId(resultSet.getLong("owner_player_id"));
                    battlePet.setPetId(resultSet.getLong("pet_id"));
                    battlePet.setHp(resultSet.getInt("hp"));
                    battlePet.setMp(resultSet.getInt("mp"));
                    battlePet.setIsActive(resultSet.getInt("is_active"));
                    battlePet.setIsAlive(resultSet.getInt("is_alive"));
                    battlePet.setPosition(resultSet.getInt("position"));
                    battlePet.setPetName(resultSet.getString("pet_name"));

                }
            }

        } catch (SQLException e) {
            StaticLog.error(e.getMessage());
            e.printStackTrace();
        }
        return battlePet;
    }

    /**
     * 根据战斗id获取当前出战的信息
     * @return
     */
    @Override
    public BattleInfoDTO getBattlePlayerInfo(Long battleId) {

        String sql = "SELECT `user`.`name` AS player_name, battle_pet.pet_name AS pet_name " +
                "FROM battle_pet " +
                "LEFT JOIN `user` ON battle_pet.owner_player_id = `user`.id " +
                "WHERE battle_pet.battle_id = ? AND battle_pet.is_active = 1";


        BattleInfoDTO battleInfoDTO = new BattleInfoDTO();

        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            // 设置参数
            stmt.setLong(1, battleId);

            // 执行查询
            try (ResultSet rs = stmt.executeQuery()) {
                int index = 0;
                while (rs.next()) {
                    String playerName = rs.getString("player_name");
                    String petName = rs.getString("pet_name");

                    if (index == 0) {
                        battleInfoDTO.setPlayer1Name(playerName);
                        battleInfoDTO.setPlayer1ActivePet(petName);
                    } else if (index == 1) {
                        battleInfoDTO.setPlayer2Name(playerName);
                        battleInfoDTO.setPlayer2ActivePet(petName);
                    }
                    index++;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return battleInfoDTO;
    }




}
